package com.penjin.android.adapter;

import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.penjin.android.R;
import com.penjin.android.domain.KaoqinPersonData;
import com.penjin.android.utils.BitmapUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by fengjw on 2016/4/9.
 */
public class GridViewAdapter extends BaseAdapter {
    private List<KaoqinPersonData> staffList = new ArrayList<>();

    Context mContext;
    public GridViewAdapter( Context context , List<KaoqinPersonData> staffList ) {
        // TODO Auto-generated constructor stub
        this.staffList = staffList;
        this.mContext = context ;
    }

    @Override
    public int getCount() {
        // TODO Auto-generated method stub
        if(staffList.size() == 0 ){
            return 0;
        }else {
            return staffList.size();
        }
    }

    @Override
    public Object getItem(int position) {
        // TODO Auto-generated method stub
        return staffList.get(position);
    }

    @Override
    public long getItemId(int position) {
        // TODO Auto-generated method stub
        return position;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        // TODO Auto-generated method stub

        GridHolder holder=null;

        if(null == convertView){
            holder=new GridHolder();
            convertView= LayoutInflater.from(mContext).inflate(R.layout.listview_kaoqinnosign_gridview_item, null);

            holder.name=(TextView) convertView.findViewById(R.id.textView1);
            holder.child_item=(RelativeLayout) convertView.findViewById(R.id.child_item);
            holder.avatar = (ImageView) convertView.findViewById(R.id.imageView1);
            convertView.setTag(holder);
        }else{
            holder=(GridHolder) convertView.getTag();
        }

        holder.name.setText(staffList.get(position).getName());
        //设置头像
        String url=staffList.get(position).getHeadUrl();
        if (url.equals("null")){
            Log.d("fengjw","url");
            holder.avatar.setImageResource(R.drawable.ease_default_avatar);
        }else {
            BitmapUtils.loadUrlImage(url, holder.avatar, mContext, true);
        }
        return convertView;
    }

    public static class GridHolder{
        TextView name;
        RelativeLayout child_item;
        ImageView avatar;
    }
}
